#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
inline int read()
{
	int x=0,f=1;
	char ch=getchar();
	while(ch<'0'||ch>'9')
	{
		if(ch=='-')f=-1;
		ch=getchar();
	}
	while(ch>='0'&&ch<='9')
	{
		x=x*10+ch-'0';
		ch=getchar();
	}
	return x*f;
}
const int MAXN=110;
int n,q,f[MAXN][MAXN];
struct point
{
	int lc,rc,v,val;
	point()
	{
		lc=rc=-1;
		v=val=0;
	}
}p[MAXN];
int treedp(int u,int k)
{
	if(u<0||k<0)return 0;
	if(~f[u][k])return f[u][k];
	int _max=0;
	for(int j=0;j<=k;++j)
	{
		int i=k-1-j,l=0,r=0;
		if(~p[u].lc)l=f[p[u].lc][i]=treedp(p[u].lc,i);
		if(~p[u].rc)r=f[p[u].rc][j]=treedp(p[u].rc,j);
		if(i<0)_max=max(_max,r);
		else _max=max(_max,p[u].val+l+r);
	}
	return _max;
}
int main()
{
	n=read();q=read();
	memset(f,-1,sizeof(f));
	for(int i=1;i<=n;++i)
	{
		int x=read();p[i].val=read();
		if(!p[x].v)p[x].lc=i;
		else p[p[x].v].rc=i;
		p[x].v=i;
	}
	for(int i=0;i<=n;++i)f[i][0]=0;
	printf("%d\n",treedp(0,q+1));
	return 0;
}
